'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

' Interaction logic for MessageWindow.xaml
Partial Public Class MessageWindow
    Inherits System.Windows.Window

    Private _ViewModel As MessageWindowViewModel

    Public Sub New(ByVal contact As ContactViewModel)
        _ViewModel = New MessageWindowViewModel(contact)

        AddHandler _ViewModel.ShowRequested, AddressOf OnShowRequested
        AddHandler _ViewModel.ConatctInformationWindowRequested, AddressOf OnConatctInformationWindowRequested
        AddHandler _ViewModel.ContactHistoryWindowRequested, AddressOf OnContactHistoryWindowRequested
        AddHandler _ViewModel.DocumentScrollRequired, AddressOf OnDocumentScrollRequired

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        App.DefaultWindowStyle.Attach(Me)

        txtMessage.Focus()
    End Sub

    Public ReadOnly Property ViewModel() As MessageWindowViewModel
        Get
            Return _ViewModel
        End Get
    End Property

    Private Sub OnShowRequested(ByVal sender As Object, ByVal e As EventArgs)
        Try
            Me.Show()
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private Sub OnConatctInformationWindowRequested(ByVal sender As Object, ByVal e As EventArgs)
        Dim wnd As ContactInformationWindow

        Try
            wnd = New ContactInformationWindow(ViewModel.Contact)
            wnd.Show()
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private Sub OnContactHistoryWindowRequested(ByVal sender As Object, ByVal e As EventArgs)
        Dim wnd As HistoryWindow

        Try
            wnd = New HistoryWindow(ViewModel.Contact)
            wnd.Show()
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private Sub OnShowContactInformationClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Try
            ViewModel.RequestContactInformationWindow()
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private Sub OnShowChatHistoryClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Try
            ViewModel.RequestChatHistoryWindow()
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private Sub OnSendMessageClick(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim messageText As String

        Try
            messageText = txtMessage.Text

            ViewModel.SendMessage(messageText)

            Me.txtMessage.Clear()
            Me.txtMessage.Focus()
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private Sub MessageWindow_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Try
            txtMessage.Focus()
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private Sub MessageWindow_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closed
        Try
            ViewModel.Close()
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private Sub OnDocumentScrollRequired(ByVal sender As Object, ByVal e As EventArgs)
        Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Normal, _
                          New Action(AddressOf MessagesRichTextBox.ScrollToEnd))
    End Sub

    Private Sub txtMessage_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles txtMessage.TextChanged
        Try
            ViewModel.SendTypingNotification(If(String.IsNullOrEmpty(txtMessage.Text), TextChangedAction.Cleared, TextChangedAction.Changed))
            Debug.WriteLine("Text changed to : {0}", txtMessage.Text)
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub
End Class